package com.jb.web.bean;

import com.jb.framework.PersistentContext;
import com.jb.web.context.WebContext;

import java.util.List;

import com.jb.faces.component.UIComponent;
import com.jb.faces.context.FacesContext;

public class TabbedPanel extends Region {
    static final long serialVersionUID = 123123L;
    static {
        publish(TabbedPanel.class, STYLE, "hideTabs");
        publish(TabbedPanel.class, STYLE, "headerAlign");
        publish(TabbedPanel.class, STYLE, "tabPosition");
    }
 
    private boolean hideTabs;

    private Panel itemSelected = null;
    private String headerAlign;
    private String tabPosition="top";//top/bottom/left/right
    
    public List getChildPanels() {
        return getChildrenByType(com.jb.web.bean.Panel.class);
    }

    //this is called when created by the tool
    public void setDefault(PersistentContext pCtx) {
        setCursor("default");
        setStyleClass("jb_tab_container");
        this.setStretch("vertical");
        for (int i = 1; i < 4; i++) {
            Panel panel = new Panel();

            panel.setVisible("true");

            if (i == 1) {
                itemSelected = panel;
            }

            panel.setId(getId() + i);
            panel.setSequence(i);
            panel.setStretch("vertical");
            panel.setHeight("300px");
            panel.setStyleClass("jb_tab_panel");
            panel.getHeader().setValue("Panel " + i);
            addChild(panel);
        }
    }

    public void setHeaderAlign(String pHeaderAlign) {
        this.headerAlign = pHeaderAlign;
    }

    public String getHeaderAlign() {
        return headerAlign;
    }

    //sets current tab
    public void setSelectTab(int tab) {
        List tabs = this.getChildrenByType(Panel.class);

        for (int i = 0; i < tabs.size(); i++) {
            Panel iPanel = (Panel) tabs.get(i);

            if (iPanel.getSequence() == tab) {
                this.setSelectedItem(iPanel);

                return;
            }
        }
    }

    public int getSelectTab() {
        return (this.getSelected() != null) ? this.getSelected().getSequence() : 0;
    }

    public Panel getSelected() {
        if (itemSelected != null) {
            return itemSelected;
        }

        List subPanels = getChildPanels();

        if (subPanels.size() == 0) {
            return null;
        }

        return (Panel) subPanels.get(0);
    }

    public boolean isSelected(HtmlItem item) {
        return item == getSelected();
    }

    
    public void setSelectedItem(HtmlItem item) {
        itemSelected = (Panel) item;
    }

    public void processUpdates(FacesContext ctx) {
        super.processUpdates(ctx);

        UIComponent comp = ((WebContext) ctx).getRequestComponent();

        if ((comp != null) && comp instanceof Panel && (comp.getParent() == this)) {
            itemSelected = (Panel) comp;
        }
    }

    protected void init() {
        super.init();
        setValue("Panel");
    }

    protected void updateChildrenView(WebContext pCtx) {
        if (itemSelected != null) {
            itemSelected.updateView(pCtx);
        } else {
            super.updateChildrenView(pCtx);
        }
    }

    public void setHideTabs(boolean hideTabs) {
        this.hideTabs = hideTabs;
    }

    public boolean isHideTabs() {
        return hideTabs;
    }

    public void setTabPosition(String param) {
        this.tabPosition = param;
    }

    public String getTabPosition() {
        return tabPosition;
    }
}
